package com.ruoyi.traveladmin.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.traveladmin.mapper.TaTravelPhotoMapper;
import com.ruoyi.traveladmin.service.ITaTravelPhotoService;
import com.ruoyi.traveladmin.domain.TaTravelPhoto;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Value;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.traveladmin.domain.TaTravel;
import com.ruoyi.traveladmin.service.ITaTravelService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Map;

/**
 * @Description  
 * @Author kemonvvv
 * @Date 2023-06-07 
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class TaTravelPhotoServiceImpl implements ITaTravelPhotoService {


    @Resource
    private TaTravelPhotoMapper taTravelPhotoMapper;
    
    
    @Resource
    private ITaTravelService taTravelService;

    
    
    /**
     * 全查询
     * 
     * @param pageDomain 分页信息
     * @param taTravelPhoto 要查询的实体类
     * @return 结果
     */
    @Override
    public Page<TaTravelPhoto> getTaTravelPhotoPage(PageDomain pageDomain, TaTravelPhoto taTravelPhoto , TaTravel taTravel) {
        if (Objects.isNull(taTravelPhoto)) {
            return new Page<>();
        }
        try {
            LambdaQueryWrapper<TaTravelPhoto> queryWrapper = new LambdaQueryWrapper<>(new TaTravelPhoto());
            //if(!Objects.isNull(taTravelPhoto.getName())){
            //	queryWrapper.like(TaTravelPhoto::getName,taTravelPhoto.getName());
            //}
            

            if (!Objects.isNull(taTravelPhoto.getId())) {
                queryWrapper.eq(TaTravelPhoto::getId, taTravelPhoto.getId());
            }

            if (!Objects.isNull(taTravelPhoto.getTaTravelId())) {
                queryWrapper.eq(TaTravelPhoto::getTaTravelId, taTravelPhoto.getTaTravelId());
            }

            taTravel.setBaseEntityNull();
            taTravel.setId(null);
            if (!PageDomain.checkObjAllFieldsIsNull(taTravel)) {
                List<Long> idList = taTravelService.getIdListByTaTravel(taTravel);
                queryWrapper.in(TaTravelPhoto::getTaTravelId, idList);
            }


            if (!Objects.isNull(taTravelPhoto.getPhotoUrl())) {
                queryWrapper.like(TaTravelPhoto::getPhotoUrl, taTravelPhoto.getPhotoUrl());
            }

            if (!Objects.isNull(taTravelPhoto.getStatus())) {
                queryWrapper.like(TaTravelPhoto::getStatus, taTravelPhoto.getStatus());
            }

            if (!Objects.isNull(taTravelPhoto.getDelFlag())) {
                queryWrapper.like(TaTravelPhoto::getDelFlag, taTravelPhoto.getDelFlag());
            }

            if (!Objects.isNull(taTravelPhoto.getCreateBy())) {
                queryWrapper.like(TaTravelPhoto::getCreateBy, taTravelPhoto.getCreateBy());
            }

            if (!Objects.isNull(taTravelPhoto.getParams().get("CreateTimeBeginTime"))||!Objects.isNull(taTravelPhoto.getParams().get("CreateTimeEndTime"))) {
                if (Objects.isNull(taTravelPhoto.getParams().get("CreateTimeBeginTime"))){
                    queryWrapper.lt(TaTravelPhoto::getCreateTime, taTravelPhoto.getParams().get("CreateTimeEndTime"));
                }
                if (Objects.isNull(taTravelPhoto.getParams().get("CreateTimeEndTime"))){
                    queryWrapper.gt(TaTravelPhoto::getCreateTime, taTravelPhoto.getParams().get("CreateTimeBeginTime"));
                }
                if (!Objects.isNull(taTravelPhoto.getParams().get("CreateTimeBeginTime")) && !Objects.isNull(taTravelPhoto.getParams().get("CreateTimeEndTime"))) {
                    queryWrapper.between(TaTravelPhoto::getCreateTime, taTravelPhoto.getParams().get("CreateTimeBeginTime"), taTravelPhoto.getParams().get("CreateTimeEndTime"));
                }
            }

            if (!Objects.isNull(taTravelPhoto.getUpdateBy())) {
                queryWrapper.like(TaTravelPhoto::getUpdateBy, taTravelPhoto.getUpdateBy());
            }

            if (!Objects.isNull(taTravelPhoto.getParams().get("UpdateTimeBeginTime"))||!Objects.isNull(taTravelPhoto.getParams().get("UpdateTimeEndTime"))) {
                if (Objects.isNull(taTravelPhoto.getParams().get("UpdateTimeBeginTime"))){
                    queryWrapper.lt(TaTravelPhoto::getUpdateTime, taTravelPhoto.getParams().get("UpdateTimeEndTime"));
                }
                if (Objects.isNull(taTravelPhoto.getParams().get("UpdateTimeEndTime"))){
                    queryWrapper.gt(TaTravelPhoto::getUpdateTime, taTravelPhoto.getParams().get("UpdateTimeBeginTime"));
                }
                if (!Objects.isNull(taTravelPhoto.getParams().get("UpdateTimeBeginTime")) && !Objects.isNull(taTravelPhoto.getParams().get("UpdateTimeEndTime"))) {
                    queryWrapper.between(TaTravelPhoto::getUpdateTime, taTravelPhoto.getParams().get("UpdateTimeBeginTime"), taTravelPhoto.getParams().get("UpdateTimeEndTime"));
                }
            }

            if (!Objects.isNull(taTravelPhoto.getRemark())) {
                queryWrapper.like(TaTravelPhoto::getRemark, taTravelPhoto.getRemark());
            }
        Page<TaTravelPhoto> taTravelPhotoList= taTravelPhotoMapper.selectPage(pageDomain.getPage(), queryWrapper);
        for(TaTravelPhoto record:taTravelPhotoList.getRecords()){
            record.setTaTravel(taTravelService.getTaTravelById(record.getTaTravelId()));
        }

            return taTravelPhotoList;
        } catch (Exception e) {
            e.printStackTrace();
            return new Page<>();
        }
    }
    

    
    /**
     * 查询通过id
     * 
     * @param id 实体类的主键
     * @return 结果
     */
    @Override
    public TaTravelPhoto getTaTravelPhotoById(Long id) {
        if (Objects.isNull(id)) {
            return new TaTravelPhoto();
        }
        try {
            TaTravelPhoto taTravelPhoto = taTravelPhotoMapper.selectById(id);
            taTravelPhoto.setTaTravel(taTravelService.getTaTravelById(taTravelPhoto.getTaTravelId()));
            if (Objects.isNull(taTravelPhoto)){
                taTravelPhoto = new TaTravelPhoto();
            }
            return taTravelPhoto;

        } catch (Exception e) {
            e.printStackTrace();
            return new TaTravelPhoto();
        }
    }


    
    
    /**
     * 通过类查询的属性值 id列表
     * 
     * @param taTravelPhoto 要查询的实体类
     * @return 结果
     */
    @Override
    public List<Long> getIdListByTaTravelPhoto(TaTravelPhoto taTravelPhoto) {
        if (Objects.isNull(taTravelPhoto)) {
            return new ArrayList<>();
        }
        try {
            LambdaQueryWrapper<TaTravelPhoto> queryWrapper = new LambdaQueryWrapper<>(new TaTravelPhoto());
            

            if (!Objects.isNull(taTravelPhoto.getId())) {
                queryWrapper.eq(TaTravelPhoto::getId, taTravelPhoto.getId());
            }

            if (!Objects.isNull(taTravelPhoto.getTaTravelId())) {
                queryWrapper.eq(TaTravelPhoto::getTaTravelId, taTravelPhoto.getTaTravelId());
            }

            if (!Objects.isNull(taTravelPhoto.getPhotoUrl())) {
                queryWrapper.like(TaTravelPhoto::getPhotoUrl, taTravelPhoto.getPhotoUrl());
            }

            if (!Objects.isNull(taTravelPhoto.getStatus())) {
                queryWrapper.like(TaTravelPhoto::getStatus, taTravelPhoto.getStatus());
            }

            if (!Objects.isNull(taTravelPhoto.getDelFlag())) {
                queryWrapper.like(TaTravelPhoto::getDelFlag, taTravelPhoto.getDelFlag());
            }

            if (!Objects.isNull(taTravelPhoto.getCreateBy())) {
                queryWrapper.like(TaTravelPhoto::getCreateBy, taTravelPhoto.getCreateBy());
            }

            if (!Objects.isNull(taTravelPhoto.getParams().get("CreateTimeBeginTime"))||!Objects.isNull(taTravelPhoto.getParams().get("CreateTimeEndTime"))) {
                if (Objects.isNull(taTravelPhoto.getParams().get("CreateTimeBeginTime"))){
                    queryWrapper.lt(TaTravelPhoto::getCreateTime, taTravelPhoto.getParams().get("CreateTimeEndTime"));
                }
                if (Objects.isNull(taTravelPhoto.getParams().get("CreateTimeEndTime"))){
                    queryWrapper.gt(TaTravelPhoto::getCreateTime, taTravelPhoto.getParams().get("CreateTimeBeginTime"));
                }
                if (!Objects.isNull(taTravelPhoto.getParams().get("CreateTimeBeginTime")) && !Objects.isNull(taTravelPhoto.getParams().get("CreateTimeEndTime"))) {
                    queryWrapper.between(TaTravelPhoto::getCreateTime, taTravelPhoto.getParams().get("CreateTimeBeginTime"), taTravelPhoto.getParams().get("CreateTimeEndTime"));
                }
            }

            if (!Objects.isNull(taTravelPhoto.getUpdateBy())) {
                queryWrapper.like(TaTravelPhoto::getUpdateBy, taTravelPhoto.getUpdateBy());
            }

            if (!Objects.isNull(taTravelPhoto.getParams().get("UpdateTimeBeginTime"))||!Objects.isNull(taTravelPhoto.getParams().get("UpdateTimeEndTime"))) {
                if (Objects.isNull(taTravelPhoto.getParams().get("UpdateTimeBeginTime"))){
                    queryWrapper.lt(TaTravelPhoto::getUpdateTime, taTravelPhoto.getParams().get("UpdateTimeEndTime"));
                }
                if (Objects.isNull(taTravelPhoto.getParams().get("UpdateTimeEndTime"))){
                    queryWrapper.gt(TaTravelPhoto::getUpdateTime, taTravelPhoto.getParams().get("UpdateTimeBeginTime"));
                }
                if (!Objects.isNull(taTravelPhoto.getParams().get("UpdateTimeBeginTime")) && !Objects.isNull(taTravelPhoto.getParams().get("UpdateTimeEndTime"))) {
                    queryWrapper.between(TaTravelPhoto::getUpdateTime, taTravelPhoto.getParams().get("UpdateTimeBeginTime"), taTravelPhoto.getParams().get("UpdateTimeEndTime"));
                }
            }

            if (!Objects.isNull(taTravelPhoto.getRemark())) {
                queryWrapper.like(TaTravelPhoto::getRemark, taTravelPhoto.getRemark());
            }

            List<TaTravelPhoto> taTravelPhotos = taTravelPhotoMapper.selectList(queryWrapper);
            List<Long> idList = new ArrayList<>();
            for (TaTravelPhoto taTravelPhotoNo:taTravelPhotos){
                idList.add(taTravelPhotoNo.getId());
            }
            return idList;
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }


    /**
     * 通过taTravel外键idList查询idList
     *
     * @param taTravelIdList 要查询的实体类
     * @return 结果
     **/
    public List<Long> getIdListByTaTravelIdList(List<Long> taTravelIdList) {
        if (Objects.isNull(taTravelIdList)){
            return  new ArrayList<>();
        }
        try{
            LambdaQueryWrapper<TaTravelPhoto> queryWrapper = new LambdaQueryWrapper<>(new TaTravelPhoto());
            queryWrapper.in(TaTravelPhoto::getTaTravelId, taTravelIdList);
            List<TaTravelPhoto> taTravelPhotos = taTravelPhotoMapper.selectList(queryWrapper);
            List<Long> idList = new ArrayList<>();
            for (TaTravelPhoto taTravelPhoto:taTravelPhotos){
                idList.add(taTravelPhoto.getId());
            }
            return idList;
        }catch (Exception e){
            return new ArrayList<>();
        }
    }

    
    /**
     * 添加
     * 
     * @param taTravelPhoto 要添加的实体类
     * @return 结果
     */
    @Override
    public int insertTaTravelPhoto(TaTravelPhoto taTravelPhoto) {
        if (Objects.isNull(taTravelPhoto)) {
            return -1;
        }
        try {
        
            //taTravelPhoto.setSpell(PinYin.getPinYin(taTravelPhoto.getName()));
            return taTravelPhotoMapper.insert(taTravelPhoto);
        } catch (Exception e) {
            return -1;
        }
    }

    /**
     * 更新通过id进行更新一条
     * 
     * @param taTravelPhoto 实体类
     * @return 结果
     */
    @Override
    public int updateTaTravelPhotoById(TaTravelPhoto taTravelPhoto) {
        if (Objects.isNull(taTravelPhoto)) {
            return -1;
        }
        try {
            //taTravelPhoto.setSpell(PinYin.getPinYin(taTravelPhoto.getName()));
            return taTravelPhotoMapper.updateById(taTravelPhoto);
        } catch (Exception e) {
            return -1;
        }
    }
    
    /**
     * 删除通过id集合
     * 
     * @param idList id集合
     * @return 结果
     */
    @Override
    public int deleteTaTravelPhotoByIdList(List idList){
        if (Objects.isNull(idList)) {
            return -1;
        }
        try {
            return taTravelPhotoMapper.deleteBatchIds(idList);
        } catch (Exception e) {
            return -1;
        }
    }
    
    /**
     * 查询无条件全部 用于导出
     * 
     * @return 结果
     */
    @Override
    public List<TaTravelPhoto> getTaTravelPhotoList() {
        try {
            LambdaQueryWrapper<TaTravelPhoto> queryWrapper = new LambdaQueryWrapper<>(new TaTravelPhoto());
            queryWrapper.isNotNull(TaTravelPhoto::getId);
            return taTravelPhotoMapper.selectList(queryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }

}
